package com.atguigu.spzx.product.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.spzx.model.dto.product.ProductSkuDto;
import com.atguigu.spzx.model.pojo.product.Product;
import com.atguigu.spzx.model.pojo.product.ProductSku;
import com.atguigu.spzx.model.pojo.product.ProductSpec;
import com.atguigu.spzx.model.request.product.ProductDetails;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.atguigu.spzx.product.mapper.*;
import com.atguigu.spzx.product.service.ProductService;
import com.atguigu.spzx.utils.DYLAssert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductSpecMapper productSpecMapper;


    /*
     * @description: 根据销量查询商品sku并且只展示通过审核的商品
     **/
    @Override
    public List<ProductSku> getProductSkuBySale() {
        return productSkuMapper.getProductSkuBySale();
    }

    @Override
    public PageInfo<ProductSku> findPage(Integer pageNum, Integer pageSize, ProductSkuDto productSkuDto) {
        //TODO 2025/1/23 测试一下mybatis3.5.8以上的版本是否和pagehelper1.4.7或以上等高版本兼容，这样的化就不需要使用mybatisplus的分页了。
//        Page<ProductSku> page = new Page<>(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSku> productSkuList = productSkuMapper.findPage(productSkuDto);
        return new PageInfo<>(productSkuList);
    }

    @Override
    public ProductSku getBySkuId(Integer skuId) {
        return productSkuMapper.selectById(skuId);
    }

    @Override
    public ProductItemVo getProductItem(Integer skuId) {
        ProductItemVo productItemVo = new ProductItemVo();
        // 商品sku
        productItemVo.setProductSku(productSkuMapper.selectById(skuId));

        // 商品信息
        // 商品分类名称
        productItemVo.setProduct(productSkuMapper.getProductBySkuId(skuId));
        Product product = productItemVo.getProduct();
        product.setCategoryName1(categoryMapper.selectById(product.getCategory1Id()).getName());
        product.setCategoryName2(categoryMapper.selectById(product.getCategory2Id()).getName());
        product.setCategoryName3(categoryMapper.selectById(product.getCategory3Id()).getName());
        // 商品sku列表
        product.setProductSkuList(productSkuMapper.selectList(new QueryWrapper<ProductSku>().eq("product_id", product.getId())));
        // 商品详细图
        product.setDetailsImageUrls(productDetailsMapper.selectOne(new QueryWrapper<ProductDetails>().eq("product_id", product.getId())).getImageUrls());


        // 轮播图
        String sliderUrls = productItemVo.getProduct().getSliderUrls();
        productItemVo.setSliderUrlList(Arrays.asList(sliderUrls.split(",")));
        // 详细信息
        ProductDetails productDetails = productDetailsMapper.selectOne(new QueryWrapper<ProductDetails>().eq("product_id", productItemVo.getProduct().getId()));
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));

        // 商品类别JSON格式
        JSONArray jsonArray = JSONArray.parseArray(product.getSpecValue());
        productItemVo.setSpecValueList(jsonArray);
        System.out.println(jsonArray);

        // 商品类别JSON map格式
        // 同一个商品下面的sku信息列表
        List<ProductSku> productSkuList = product.getProductSkuList();
        // 建立sku规格与skuId对应关系
        Map<String, Object> skuSpecValueMap = new HashMap<>();
        //TODO 2025/1/24  这里的map，奇怪
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);
        return productItemVo;
    }

    @Override
    public void updateStormAndSale(Long skuId, Integer skuNum) {
        ProductSku sku = productSkuMapper.selectById(skuId);
        DYLAssert.isTrue(sku.getStockNum() - skuNum > 0,"库存不足");
        sku.setStockNum(sku.getStockNum() - skuNum);
        sku.setSaleNum(sku.getSaleNum() + skuNum);
        productSkuMapper.updateById(sku);
    }

}
